import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:platform_service/platform_service.dart';

import 'common_scanner.dart';
import 'scan_page.dart';

class ScannerExample extends StatefulWidget {
  const ScannerExample({super.key});

  static Future push(BuildContext context) =>
      Navigator.of(context).push(PageRouteBuilder(
        settings: const RouteSettings(name: 'ScannerExample'),
        pageBuilder: (context, animation, secondaryAnimation) =>
            const ScannerExample(),
      ));

  @override
  State<ScannerExample> createState() => _ScannerExampleState();
}

class _ScannerExampleState extends State<ScannerExample> {
  String qrCode = '';

  @override
  void initState() {
    super.initState();
    PdaScanner.enable();
    PdaScanner.startPDAScan(
        context: context,
        onScanResult: (result) {
          setState(() {
            qrCode = result ?? '';
          });
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Plugin example app'),
      ),
      body: Center(
        child: Column(
          children: [
            CupertinoButton(
                child: const Text("开启扫码"),
                onPressed: () {
                  Navigator.of(context)
                      .push(MaterialPageRoute(builder: (context) {
                    return const ScannerPage();
                  }));
                }),
            ElevatedButton(
                onPressed: () {
                  Navigator.of(context).push(MaterialPageRoute(
                      builder: (context) => const ScannerPage(
                            continueQr: true,
                          )));
                },
                child: const Text('连续扫码')),
            Text('PDA扫描结果： $qrCode'),
            const SizedBox(
              height: 14,
            ),
            ElevatedButton(
                onPressed: () {
                  Navigator.of(context).push(MaterialPageRoute(
                      builder: (context) => const CommonScannerPage()));
                },
                child: const Text('通用扫码'))
          ],
        ),
      ),
    );
  }
}
